var tests = [
        "test_formhandler_shouldValidateEmailAddresses",
        "test_formhandler_shouldValidateURLs",
        "test_formhandler_shouldValidatePIN",
        "test_formhandler_shouldValidateAlias",
        "test_formhandler_shouldValidatePhonenumber",
        "test_formhandler_shouldValidateInteger",
        "test_formhandler_shouldValidateUsername",
        "test_formhandler_shouldValidatePercent",
        "test_formhandler_shouldValidateMoney",
        "test_formhandler_shouldValidateDate",
        "test_formhandler_shouldValidateTime",
        "test_formhandler_shouldCreateAndRemoveForm"
];

var test_formhandler_shouldEvaluteFields = function() {
    var formID = tenez.FormHandler.registerNewForm();
    var form = tenez.FormHandler.getForm(formID);

    form.add("emailfield", "MESSAGE1", "MISSING1", "email", true);
    form.add("aliasfield", "MESSAGE2", "MISSING2", "alias", true);
    form.add("phonefield", "MESSAGE3", "MISSING3", "phone", true);

    req.data.emailfield = "philipp.naderer..@@invalid.com";
    req.data.aliasfield = "";
    req.data.phonefield = "0900 IPHONE IPHONE IPHONE";

    assertFalse(form.evaluate());
    form.addMessage("ADDEDMESSAGE3");

    var stack = form.getMessageStack();
    assertEqual("MESSAGE1", stack[0]);
    assertEqual("MISSING2", stack[1]);
    assertEqual("MESSAGE3", stack[2]);
    assertEqual("ADDEDMESSAGE3", stack[3]);

    tenez.FormHandler.remove(form);
    assertUndefined(tenez.FormHandler.getForm(formID));

    return;
};

var test_formhandler_shouldCreateAndRemoveForm = function() {
    var formID = tenez.FormHandler.registerNewForm();
    var form   = tenez.FormHandler.getForm(formID);

    assertNotNull(form);
    assertEqual(form.getId(), formID);
    assertEqual(res.handlers.form, form);
    assertFalse(form.isDirty());
    assertEqual("[FormHandler.Form #" + formID + "]", form.toString());

    tenez.FormHandler.remove(form);
    assertUndefined(tenez.FormHandler.getForm(formID));

    return;
};

var test_formhandler_shouldValidateEmailAddresses = function() {

    var validator = tenez.FormHandler.VALIDATORS["email"];

    assertTrue(validator("naderer.testmail@tenez.at"));
    assertTrue(validator("naderer@tenez.biz"));
    assertTrue(validator("iam.a.testuser.called.philipp@subdomain.tenez.at"));

    assertFalse(validator("abc.tenez.at"));
    assertFalse(validator("nodotatend.@tenez.at"));
    assertFalse(validator("doubledot..@tenez.at"));
    assertFalse(validator("<asdf>@tenez.at"));
    assertFalse(validator("()[]\;:,<>@tenez.at"));

    return;
};

var test_formhandler_shouldValidateURLs = function() {

    var validator = tenez.FormHandler.VALIDATORS["url"];

    assertTrue(validator("http://tenez.at"));
    assertTrue(validator("https://tenez.at"));
    assertTrue(validator("http://subdomain.subsubdomain.tenez.at"));
    assertTrue(validator("https://subdomain.subsubdomain.tenez.at"));
    assertTrue(validator("http://domain.tld:8080/filepathname?query_string#anchor"));


    assertFalse(validator("http://tldmissing:8080/filepathname?query_string#anchor"));
    assertFalse(validator("abc.tenez.at"));
    assertFalse(validator("nodotatend.@tenez.at"));
    assertFalse(validator("doubledot..@tenez.at"));
    assertFalse(validator("<asdf>@tenez.at"));
    assertFalse(validator("()[]\;:,<>@tenez.at"));

    return;
};

var test_formhandler_shouldValidatePIN = function() {
    var validator = tenez.FormHandler.VALIDATORS["pin"];

    assertTrue(validator("asdfghjklqwertzuiopyxcvbnm1234567890"));
    assertFalse(validator("§"));
    assertFalse(validator("!"));
    assertFalse(validator("$"));
    assertFalse(validator("%"));
    assertFalse(validator("&"));
    assertFalse(validator("/"));
    assertFalse(validator("("));
    assertFalse(validator(")"));
    assertFalse(validator("["));
    assertFalse(validator("]"));
    assertFalse(validator("="));
    assertFalse(validator("*"));
    assertFalse(validator("+"));
    assertFalse(validator("#"));
    assertFalse(validator("-"));
    assertFalse(validator("."));
    assertFalse(validator(":"));
    assertFalse(validator(";"));
};

var test_formhandler_shouldValidateAlias = function() {
    var validator = tenez.FormHandler.VALIDATORS["alias"];

    assertTrue(validator("asdfghjkl_qwert.zuiopyxcvbnm-1234567890"));
    assertFalse(validator("§"));
    assertFalse(validator("!"));
    assertFalse(validator("$"));
    assertFalse(validator("%"));
    assertFalse(validator("&"));
    assertFalse(validator("/"));
    assertFalse(validator("("));
    assertFalse(validator(")"));
    assertFalse(validator("["));
    assertFalse(validator("]"));
    assertFalse(validator("="));
    assertFalse(validator("*"));
    assertFalse(validator("+"));
    assertFalse(validator("#"));
    assertFalse(validator(":"));
    assertFalse(validator(";"));
    assertFalse(validator("ö"));
    assertFalse(validator("ü"));
    assertFalse(validator("ä"));
};

var test_formhandler_shouldValidatePhonenumber = function() {
    var validator = tenez.FormHandler.VALIDATORS["phone"];

    assertTrue(validator("+43 7475 12345"));
    assertTrue(validator("+43 7475/12345"));
    assertTrue(validator("(0043) 7475 12345 6677"));
    assertTrue(validator("(0043) 7475 12345 6677-2"));
    assertTrue(validator("+40 12345 ext 12345"));
    assertTrue(validator("+40 12345 x 12345"));
    assertTrue(validator("+40 12345-123-45"));
    assertFalse(validator("§"));
    assertFalse(validator("!"));
    assertFalse(validator("$"));
    assertFalse(validator("%"));
    assertFalse(validator("&"));
    assertFalse(validator("["));
    assertFalse(validator("]"));
    assertFalse(validator("="));
    assertFalse(validator("#"));
    assertFalse(validator(":"));
    assertFalse(validator(";"));
    assertFalse(validator(","));
    assertFalse(validator("asdfjklqwweroi,mewr"));
};

var test_formhandler_shouldValidateInteger = function() {
    var validator = tenez.FormHandler.VALIDATORS["integer"];

    assertTrue(validator("12345"));
    assertTrue(validator("00000123"));
    assertFalse(validator("1.1"));
    assertFalse(validator("1,1"));
    assertFalse(validator("0,1"));
    assertFalse(validator("0.1"));
    assertFalse(validator("§"));
    assertFalse(validator("!"));
    assertFalse(validator("$"));
    assertFalse(validator("%"));
    assertFalse(validator("&"));
    assertFalse(validator("["));
    assertFalse(validator("]"));
    assertFalse(validator("="));
    assertFalse(validator("#"));
    assertFalse(validator(":"));
    assertFalse(validator(";"));
    assertFalse(validator(","));
    assertFalse(validator("ABCDEF"));
};

var test_formhandler_shouldValidateUsername = function() {
    var validator = tenez.FormHandler.VALIDATORS["username"];

    assertTrue(validator("asdfghjkl_qwert.zuiopyxcvbnm-1234567890"));
    assertFalse(validator("§"));
    assertFalse(validator("!"));
    assertFalse(validator("$"));
    assertFalse(validator("%"));
    assertFalse(validator("&"));
    assertFalse(validator("/"));
    assertFalse(validator("("));
    assertFalse(validator(")"));
    assertFalse(validator("["));
    assertFalse(validator("]"));
    assertFalse(validator("="));
    assertFalse(validator("*"));
    assertFalse(validator("+"));
    assertFalse(validator("#"));
    assertFalse(validator(":"));
    assertFalse(validator(";"));
    assertFalse(validator("ö"));
    assertFalse(validator("ü"));
    assertFalse(validator("ä"));
};

var test_formhandler_shouldValidatePercent = function() {
    var validator = tenez.FormHandler.VALIDATORS["percent"];

    assertTrue(validator("0.0"));
    assertTrue(validator("0.00"));
    assertTrue(validator("0"));
    assertTrue(validator("1.0"));
    assertTrue(validator("10.0"));
    assertTrue(validator("10.25"));
    assertTrue(validator("50.55"));
    assertTrue(validator("100"));
    assertTrue(validator("100.00"));

    assertFalse(validator("-1"));
    assertFalse(validator("-1.1"));
    assertFalse(validator("1,1"));
    assertFalse(validator("1,0"));
    assertFalse(validator("100.1"));
    assertFalse(validator("100,1"));
    assertFalse(validator("100,01"));
    assertFalse(validator("100.01"));
    assertFalse(validator("100.02"));
    assertFalse(validator("1000"));

    // Invalid formats
    assertFalse(validator("1.233"));
    assertFalse(validator("1,233"));
    assertFalse(validator("10.123"));
    assertFalse(validator("10,123"));
    assertFalse(validator("12]"));
    assertFalse(validator("12="));
    assertFalse(validator("17*"));
    assertFalse(validator("12;12"));
    assertFalse(validator("12#"));
    assertFalse(validator("12:12"));
    assertFalse(validator("A"));
    assertFalse(validator("B"));
    assertFalse(validator("C"));
    assertFalse(validator("D"));
    assertFalse(validator("E"));
    assertFalse(validator("F"));
};

var test_formhandler_shouldValidateMoney = function() {
    var validator = tenez.FormHandler.VALIDATORS["money"];

    // Valid Formats
    assertTrue(validator("0.0"));
    assertTrue(validator("0.00"));
    assertTrue(validator("0"));
    assertTrue(validator("1.0"));
    assertTrue(validator("10.0"));
    assertTrue(validator("10.25"));
    assertTrue(validator("50.55"));
    assertTrue(validator("1000"));
    assertTrue(validator("100.01"));
    assertTrue(validator("100.02"));
    assertTrue(validator("1,1"));
    assertTrue(validator("1,0"));
    assertTrue(validator("100,1"));
    assertTrue(validator("100,01"));
    assertTrue(validator("100.1"));
    
    // Invalid formats
    assertFalse(validator("-1"));
    assertFalse(validator("-1.1"));
    assertFalse(validator("1,233"));
    assertFalse(validator("1.233"));
    assertFalse(validator("10,123"));
    assertFalse(validator("10.123"));
    assertFalse(validator("12]"));
    assertFalse(validator("12="));
    assertFalse(validator("17*"));
    assertFalse(validator("12;12"));
    assertFalse(validator("12#"));
    assertFalse(validator("12:12"));
    assertFalse(validator("A"));
    assertFalse(validator("B"));
    assertFalse(validator("C"));
    assertFalse(validator("D"));
    assertFalse(validator("E"));
    assertFalse(validator("F"));
};

var test_formhandler_shouldValidateDate = function() {
    var validator = tenez.FormHandler.VALIDATORS["date"];
    
    // Valid Formats
    assertTrue(validator("20.01.2010"));
    assertTrue(validator("28.02.2010"));
    assertTrue(validator("01.01.1999"));
    assertTrue(validator("1.1.1999"));
    assertTrue(validator("31.12.2099"));
    assertTrue(validator("26.9.1986"));
    assertTrue(validator("26.08.2010"));
    assertTrue(validator("26.09.2010"));
    assertTrue(validator("29.02.2008"));
    assertTrue(validator("29.02.2004"));
    assertTrue(validator("29.02.2000"));
    assertTrue(validator("29.02.1996"));
    
    // Invalid formats
    assertFalse(validator("20-01-2010"));
    assertFalse(validator("28-02-2010"));
    assertFalse(validator("29-02-2008"));
    assertFalse(validator("28-02-2010"));
    assertFalse(validator("30.2.2010"));
    assertFalse(validator("30.02.2010"));
    assertFalse(validator("32.12.2010"));
    assertFalse(validator("32.1.2010"));
    assertFalse(validator("2010-01-01"));
    assertFalse(validator("01.01.2010 10:10:00"));
    assertFalse(validator("01.01.2010 10:10"));
    assertFalse(validator("9.9.99"));
    assertFalse(validator("-1"));
    assertFalse(validator("A"));
};

var test_formhandler_shouldValidateTime = function() {
    var validator = tenez.FormHandler.VALIDATORS["time"];

    // Valid Formats
    assertTrue(validator("0:00"));
    assertTrue(validator("18:01"));
    assertTrue(validator("06:01"));
    assertTrue(validator("6:01"));
    assertTrue(validator("0:10"));
    assertTrue(validator("1:11"));
    assertTrue(validator("11:11"));
    
    // Invalid formats
    assertFalse(validator("0-00"));
    assertFalse(validator("18.01"));
    assertFalse(validator("06/01"));
    assertFalse(validator("6%01"));
    assertFalse(validator("24:00"));
    assertFalse(validator("00:60"));
    assertFalse(validator("60:1"));
    assertFalse(validator("3:1"));
    assertFalse(validator("-1:00"));
    assertFalse(validator("A"));
};
